package com.xneure.utils.http;

import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.xneure.utils.JsonUtil;

public class HttpResultProvider {
	private HttpResponse res;

	public HttpResultProvider(HttpResponse res) {
		this.res = res;
	}

	public String getValue() {
		return getValue(Consts.UTF_8);
	}

	public String getValue(Charset cSet) {
		try {
			return EntityUtils.toString(res.getEntity(), cSet);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public InputStream geInputStream(){
		try {
			return res.getEntity().getContent();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public <T> T getValue(Class<T> cls) {
		return getValue(cls, Consts.UTF_8);
	}

	public <T> T getValue(Class<T> cls, Charset cSet) {
		return JsonUtil.toObject(getValue(cSet), cls);
	}

	@SuppressWarnings("unchecked")
	public List<Object> getValueToList() {
		return JsonUtil.toObject(getValue(Consts.UTF_8),List.class);
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> getValueToMap() {
		return JsonUtil.toObject((getValue(Consts.UTF_8)),Map.class);
	}

	@SuppressWarnings("deprecation")
	public String getParam(String name) {
		return res.getParams().getParameter(name).toString();
	}

	public Header[] getHeader(String name) {
		return res.getHeaders(name);
	}

	public Header[] getHeaders() {
		return res.getAllHeaders();
	}
}
